package ee.ttu.rakarh.controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;


@SuppressWarnings("serial")
public class FrontController extends HttpServlet {

	public static final Logger LOG = Logger.getLogger(FrontController.class);
	public void init(ServletConfig config) {

	}

	public void doGet(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {


		try {
			long startTime = System.currentTimeMillis();
			RequestHandler requestHandler = new RequestHandler();
			String action = requestHandler.handle(req, res);
			
			IndexController ic = new IndexController();
			String bodyModule = ic.getBodyModule(action);
			req.setAttribute("bodyModule", bodyModule);
			
			RequestDispatcher dispatcher = req.getRequestDispatcher("index.jsp");
			dispatcher.forward(req, res);
			long totalTime = System.currentTimeMillis() - startTime;
			LOG.info("Done in " + totalTime +"ms");
			
		}

		catch (Exception ex) {
			LOG.error(ex);
		}

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {

		doGet(request, response);
	}

}
